$(function () {
  let id = window.location.search.substr(4);
  $.ajax({
    url: `http://localhost:8888/api/articles/${id}/comments`,
    method: "get",
    success: function (data) {
      console.log(data);
      $.each(data.data, (index, ele) => {
        var content = `  <div class="kr_comment_card">
        <div class="img-wrap">
          <img src="./uploads/avatar_3.jpg" alt="">
        </div>
        <div class="info">
          <p>${ele.uname} · <span>${ele.cdate.split("T")[0]}</span></p>
          <p>${ele.content}</p>
        </div>
        <a href="javascript:;" class="like">${ele.count}</a>
      </div> `;
        $(".kr_panel:eq(1)").append(content);
      });
    },
  });
  //发布评论
  $(".action button").on("click", function (e) {
    e.preventDefault();
    $.ajax({
      url: `http://localhost:8888/api/articles/${id}/comments`,
      method: "post",
      data: {
        uname: $(this).parent().siblings("input").val(),
        content: $(this).parent().siblings("textarea").val(),
      },
      success: function (data) {
        console.log(data);
        window.location.reload();
      },
    });
  });
  $(".form textarea").on({
    input: function () {
      let text = $(".word_count i").text();
      text = $(this).val().length;
      $(".word_count i").text(text);
      if ($(".word_count i").text() >= 1000) {
        alert("最多评论1000字");
      }
    },
    keyup: function (e) {
      if (e.keyCode === 13) {
        $(".action button").click();
      }
    },
  });
});
